﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Lyx.Entities.Units
{
    class Unit
    {
        public List<Module> listModules = new List<Module>();
        public Inputs.Controller controller;

        public Vector2 vVelocity = new Vector2(0.0f, 0.0f);
        float fRotation = 0.0f;

        public Unit(Module _root, Inputs.Controller _controller)
        {
            listModules.Add(_root);
            controller = _controller;
        }

        public void AddModule(Vector2 _vDecal, Tools.SpriteKey _spriteKey)
        {
            _vDecal.X *= -1.0f;
            listModules.Add(new Module(_vDecal, listModules[0].vPosition, _spriteKey));
        }

        public void Update(GameTime _gameTime)
        {
            UpdateRoot(_gameTime);

            for (int i = 1; i < listModules.Count; i++)
            {
                listModules[i].UpdatePosition(listModules[0].vPosition, listModules[0].fRotation);
                listModules[i].spriteKey.Update(_gameTime);
            }
        }

        public void CancelPosition()
        {
            vVelocity = -vVelocity / 2;
            fRotation = -fRotation / 4;

            for (int i = 0; i < listModules.Count; i++)
            {
                listModules[i].CancelPosition();
            }
        }

        public void UpdateRoot(GameTime _gameTime)
        {
            listModules[0].SaveOldPosition(listModules[0].vPosition, listModules[0].fRotation);

            if (controller != null)
            {
                fRotation += (0.0002f * controller.fRotate);
                listModules[0].fRotation += fRotation * _gameTime.ElapsedGameTime.Milliseconds;

                vVelocity.X += (float)0.02f * controller.iDirection;
                vVelocity.Y += (float)0.02f * controller.iDirection;

                listModules[0].vPosition.X += vVelocity.X * _gameTime.ElapsedGameTime.Milliseconds * (float)Math.Cos(listModules[0].fRotation + 1.57079633);
                listModules[0].vPosition.Y += vVelocity.Y * _gameTime.ElapsedGameTime.Milliseconds * (float)Math.Sin(listModules[0].fRotation + 1.57079633);
            }

            listModules[0].boundingRect.UpdatePosition(listModules[0].vPosition, listModules[0].fRotation);
            listModules[0].spriteKey.Update(_gameTime);

            if (fRotation > 0)
                fRotation -= 0.0001f;
            else if (fRotation < 0)
                fRotation += 0.0001f;

            if (vVelocity.X > 0)
                vVelocity.X -= 0.01f;
            else if (vVelocity.X < 0)
                vVelocity.X += 0.01f;

            if (vVelocity.Y > 0)
                vVelocity.Y -= 0.01f;
            else if (vVelocity.Y < 0)
                vVelocity.Y += 0.01f;
        }

        public void setPosition(Vector2 _vPosition)
        {
            listModules[0].vPosition.X = _vPosition.X;
            listModules[0].vPosition.Y = _vPosition.Y;
        }
    }
}
